<?php
declare (strict_types=1);

namespace app\exam\model;

use app\common\model\BaseModel;
use app\user\logic\user\Account as User;
use app\course\model\base\{Course, CourseExamLink};
use app\course\logic\base\{CourseCert, CourseStudent};
use Exception;

/**
 * 试卷模型
 * Author 亓官雨树<lucky.max@foxmail.com>
 * Date 2021-04-03
 */
class PaperAnswer extends BaseModel
{
    /**
     * 表名
     * @var string
     */
    protected $name = 'exam_paper_answer_record';

    /**
     * 定义字段类型
     * @var [type]
     */
    protected $type = [
        'user_id'       => 'integer',
        'paper_id'      => 'integer',
        'paper_parts'   => 'json',
        'answer_data'   => 'json',
    ];

// 模型事件

    /**
     * 成功创建之后
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2021-10-11
     *
     * @throws Exception
     */
    public static function onAfterInsert($data)
    {
        if ( !$data->rollup_id ) {
            if ( $data->course_id ) {
                // 检查是否发放证书
                app(CourseCert::class)->addCert($data->course_id, $data->user_id);
            } else {
                // 检查试卷是否所属用户所在课程
                $sql = app(CourseStudent::class)->field('course_id')->whereUserId($data->user_id)->whereRaw('expire_time >'.time().' OR expire_time is null')->buildSql();
                $ids = app(CourseExamLink::class)->wherePaperId($data->paper_id)->whereRaw('course_id IN ('.$sql.' )')->column('course_id');

                if ( count($ids) ) {
                    foreach ($ids as $id) {
                        // 检查是否发放证书
                        app(CourseCert::class)->addCert($id, $data->user_id);
                    }
                }
            }
        }
    }

//
    /**
     * 用户查询范围
     */
    public function scopeCurrent($query, $mid = 0)
    {
        $mid = $mid ? : request()->mid;
        $query->whereUserId($mid);
    }


    /**
     * 唯一标识查询范围
     */
    public function scopeUniqueCode($query, $code = '')
    {
        $query->whereUniqueCode($code);
    }


    /**
     * 试卷ID查询范围
     */
    public function scopePaperId($query, $id = 0)
    {
        $query->wherePaperId($id);
    }


    /**
     * 课程ID查询范围
     */
    public function scopeCourseId($query, $id = 0)
    {
        $query->whereCourseId($id);
    }

    /**
     * 关联试卷
     */
    public function paper()
    {
        return $this->belongsTo(Paper::class, 'paper_id');
    }

    /**
     * 关联试卷标题
     */
    public function paperTitle()
    {
        return $this->belongsTo(Paper::class, 'paper_id')->bind(['paper_title' => 'title']);
    }

    /**
     * 关联试卷交卷后显示答案
     */
    public function paperVisibleAnswer()
    {
        return $this->belongsTo(Paper::class, 'paper_id')->bind(['visible_answer']);
    }

    /**
     * 关联试卷标题
     */
    public function rollupTitle()
    {
        return $this->belongsTo(Rollup::class, 'rollup_id')->bind(['rollup_title' => 'title']);
    }

    /**
     * 关联课程标题
     */
    public function courseTitle()
    {
        return $this->belongsTo(Course::class, 'course_id')->bind(['course_title' => 'title']);
    }

    /**
     * 关联用户昵称
     */
    public function userName()
    {
        return $this->belongsTo(User::class, 'user_id')->bind(['nick_name']);
    }

}
